This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
library(car)
Warning: package ‘car’ was built under R version 4.3.3
Loading required package: carData
Warning: package ‘carData’ was built under R version 4.3.2
Attaching package: ‘car’
The following object is masked from ‘package:dplyr’:
recode
df <- read.csv("Data/df_clean_more_features.csv")
getvar <- function()
dfuk <- df[df$cntry == "United Kingdom", ]
dftotuk %>%
select(gndr, weighta, fltdpr) %>%
mutate(gndr = factor(gndr, levels = c(1, 2), labels = c("Male", "Female"))) %>%
t.test(data = ., weighta ~ gndr)
Welch Two Sample t-test
data: weighta by gndr
t = 18.571, df = 1296.6, p-value < 2.2e-16
alternative hypothesis: true difference in means between group Male and group Female is not equal to 0
95 percent confidence interval:
12.92829 15.98233
sample estimates:
mean in group Male mean in group Female
82.34348 67.88817
library(effsize)
cohen.d(df$weighta, df$gndr)
Cohen's d
d estimate: 1.027161 (large)
95 percent confidence interval:
lower upper
0.9115384 1.1427840
cohen.d(fem, mal)
Cohen's d
d estimate: -1.027161 (large)
95 percent confidence interval:
lower upper
-1.1427840 -0.9115384
mal <- dfmale[, "weighta"]
Error: unexpected symbol in:
""
mal <- dfmale[, "weighta"
df <- dftotuk[c("gndr", "weighta", "fltdpr")]
df$gndr <- factor(df$gndr, levels = c(1, 2), labels = c("Male", "Female"))
df %>%
group_by(gndr) %>%
summarise(n())
ggplot(data = df, aes(x = weighta, fill = gndr)) + geom_area(stat = "density", alpha = 0.5) + scale_fill_manual(values = c("Female" = "green", "Male" = "blue"))
cor(df$weighta, df$fltdpr)
[1] -0.003393421
Levene’s Test of homogeneity
leveneTest(data = df, weighta ~ gndr)
Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 1 0.3674 0.5445
1305
dfmale <- dfuk[dfuk$gndr == "Male", ]
dffemale <- dfuk[dfuk$gndr == "Female", ]
dfuk
#Call ggplot
library(ggplot2)
#Make the plot
ggplot(data = dfmale, aes(x = cgtsmok)) +
geom_histogram()
shapiro.test(dfmale$cgtsmok)
dfpt <- read.csv('ess_allyr_multi.csv')
unique(dfpt$name)
[1] "ESS1e06_7" "ESS2e03_6" "ESS3e03_7" "ESS4e04_6" "ESS5e03_5" "ESS6e02_6" "ESS7e02_3"
[8] "ESS8e02_3" "ESS9e03_2" "ESS10e03_2" "ESS10SCe03_1" "ESS11e02"
df <- dfpt %>%
mutate(name = case_when(
name == "ESS1e06_7" ~ "2002",
name == "ESS2e03_6" ~ "2004",
name == "ESS3e03_7" ~ "2006",
name == "ESS4e04_6" ~ "2008",
name == "ESS5e03_5" ~ "2010",
name == "ESS6e02_6" ~ "2012",
name == "ESS7e02_3" ~ "2014",
name == "ESS8e02_3" ~ "2016",
name == "ESS9e03_2" ~ "2018",
name == "ESS10e03_2" ~ "2020",
name == "ESS11e02" ~ "2023"
))
dfhappy <- df %>%
select(name, happy) %>%
filter(happy <= 10) %>%
group_by(name, happy) %>%
summarise(qty = n(), .groups = "drop") %>%
group_by(name) %>%
mutate(perc = round((qty / sum(qty)) * 100, 1))
#mutate(perc = mutate(qty/sum(qty)) *100)
dfhappy
dfhappy2 <- df %>%
select(name, happy) %>%
filter(happy <= 10) %>%
group_by(name, happy) %>%
summarise(qty = n(), .groups = "drop") %>%
group_by(name) %>%
mutate(perc = round((qty / sum(qty)) * 100, 1)) %>%
mutate(accum_freq = cumsum(perc)) %>%
mutate(accum_freq = ifelse(row_number() == n(), 100, accum_freq)) # Set last row to 100
dfhappy2
ggplot(dfhappy, aes(x = happy, y = perc, color = name, group = name)) +
geom_line(size = 1) + # Lines for each year
geom_point(size = 2) + # Points for better visibility
labs(
title = "Distribution of Happiness Scores by Year",
x = "Happiness Score",
y = "Percentage",
color = "Year"
) +
scale_x_continuous(breaks = 0:10) + # Show all happiness values on x-axis
theme_minimal()
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
This warning is displayed once every 8 hours.
Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
library(plotly)
Warning: package ‘plotly’ was built under R version 4.3.3
Registered S3 method overwritten by 'data.table':
method from
print.data.table
Attaching package: ‘plotly’
The following object is masked from ‘package:ggplot2’:
last_plot
The following object is masked from ‘package:igraph’:
groups
The following object is masked from ‘package:stats’:
filter
The following object is masked from ‘package:graphics’:
layout
# Interactive Plot with Plotly
plot <- dfhappy %>%
plot_ly(
x = ~happy,
y = ~perc,
color = ~name,
type = 'scatter',
mode = 'lines+markers',
text = ~paste("Year:", name, "<br>Happy:", happy, "<br>Percentage:", perc, "%")
) %>%
layout(
title = "Distribution of Happiness Scores by Year",
xaxis = list(title = "Happiness Score", tickvals = 0:10),
yaxis = list(title = "Percentage"),
legend = list(title = list(text = "Year"))
)
plot
Warning in RColorBrewer::brewer.pal(N, "Set2") :
n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors
Warning in RColorBrewer::brewer.pal(N, "Set2") :
n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors
Warning in RColorBrewer::brewer.pal(N, "Set2") :
n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors
Warning in RColorBrewer::brewer.pal(N, "Set2") :
n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors
plot <- dfhappy %>%
plot_ly(
x = ~happy,
y = ~perc,
color = ~name,
type = 'scatter',
mode = 'lines',
fill = 'tonexty', # Fills area below the line
text = ~paste("Year:", name, "<br>Happy:", happy, "<br>Percentage:", perc, "%")
) %>%
layout(
title = "Distribution of Happiness Scores by Year",
xaxis = list(title = "Happiness Score", tickvals = 0:10),
yaxis = list(title = "Percentage"),
legend = list(title = list(text = "Year"))
)
plot
Warning in RColorBrewer::brewer.pal(N, "Set2") :
n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors
Warning in RColorBrewer::brewer.pal(N, "Set2") :
n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors
Warning in RColorBrewer::brewer.pal(N, "Set2") :
n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors
Warning in RColorBrewer::brewer.pal(N, "Set2") :
n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors
df <- df %>%
mutate(name = case_when(
name == "ESS1e06_7" ~ "2002",
name == "ESS2e03_6" ~ "2004",
name == "ESS3e03_7" ~ "2006",
name == "ESS4e04_6" ~ "2008",
name == "ESS5e03_5" ~ "2010",
name == "ESS6e02_6" ~ "2012",
name == "ESS7e02_3" ~ "2014",
name == "ESS8e02_3" ~ "2016",
name == "ESS9e03_2" ~ "2018",
name == "ESS10e03_2" ~ "2020",
name == "ESS10SCe03_1" ~ "2020",
name == "ESS11e02" ~ "2023"
))
dfhappy2023 <- df %>%
filter(name == "2023") %>%
select(cntry, happy) %>%
filter(happy <= 10) %>%
group_by(cntry, happy) %>%
summarise(qty = n(), .groups = "drop") %>%
group_by(cntry) %>%
mutate(perc = round((qty / sum(qty)) * 100, 1)) %>%
mutate(accum_freq = cumsum(perc)) %>%
mutate(accum_freq = ifelse(row_number() == n(), 100, accum_freq)) # Set last row to 100
dfhappy2023
plot <- dfhappy2023 %>%
plot_ly(
#x = ~happy,
#y = ~perc,
y = ~happy,
x = ~perc,
color = ~cntry,
type = 'scatter',
#mode = 'lines',
mode = 'lines+markers',
#fill = 'tonexty', # Fills area below the line
text = ~paste("Country:", cntry, "<br>Happy:", happy, "<br>Percentage:", perc, "%")
) %>%
layout(
title = "Distribution of Happiness Scores by Country in 2023",
xaxis = list(title = "Happiness Score", tickvals = 0:100),
yaxis = list(title = "Percentage"),
legend = list(title = list(text = "Country"))
)
plot
Warning in RColorBrewer::brewer.pal(N, "Set2") :
n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors
Warning in RColorBrewer::brewer.pal(N, "Set2") :
n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors
Warning in RColorBrewer::brewer.pal(N, "Set2") :
n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors
Warning in RColorBrewer::brewer.pal(N, "Set2") :
n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors
unique(dfhappy$cntry)
[1] "BE" "CH" "DE" "ES" "FI" "FR" "GB" "HU" "IE" "NL" "NO" "PL" "PT" "SE" "SI"
plot <- dfhappy2023 %>%
plot_ly(
x = ~cntry, # Percentages on the x-axis
y = ~perc, # Countries on the y-axis
color = ~happy, # Color by level of happiness
type = 'bar', # Bar chart
#orientation = 'h', # Horizontal bars
#text = ~paste("Country:", cntry, "<br>Happy Level:", happy, "<br>Percentage:", perc, "%"),
#hoverinfo = 'text' # Custom hover info
text = ~happy, # Display only happiness level on the graph
hoverinfo = 'text', # Maintain hover information
hovertext = ~paste("Country:", cntry, "<br>Happy Level:", happy, "<br>Percentage:", perc, "%") # Detailed hover info
) %>%
layout(
barmode = 'stack', # Stacked bars
title = "Happiness Distribution by Level Across Countries (2023)",
xaxis = list(title = "Percentage"),
yaxis = list(title = "Country"),
legend = list(title = list(text = "Happiness Level"))
)
plot
Warning: textfont.color doesn't (yet) support data arrays
Warning: textfont.color doesn't (yet) support data arrays
Warning: textfont.color doesn't (yet) support data arrays
Warning: textfont.color doesn't (yet) support data arrays
op_cntry1 <- 'NL'
op_cntry2 <- 'PT'
op_df <- dfhappy2023 %>%
filter(cntry == op_cntry1 | cntry == op_cntry2 | cntry == 'HU' | cntry == 'FR') %>%
mutate(accum_freq = cumsum(perc)) %>%
mutate(accum_freq = ifelse(row_number() == n(), 100, accum_freq)) # Set last row to 100
op_df
# Create the Plotly graph
plot <- op_df %>%
plot_ly(
x = ~happy, # Happiness levels as the x-axis
y = ~perc, # Percentage values as the y-axis
color = ~cntry, # Different colors for each country
type = 'bar', # Bar chart type
barmode = 'group', # Grouped bars (side-by-side)
text = ~paste(
"Country:", cntry,
"<br>Happiness score:", happy,
"<br>Percentage:", perc, "%",
"<br>Accumulated Frequency:", accum_freq, "%"
), # Hover information with details
hoverinfo = 'text' # Enable hover with custom text
) %>%
layout(
title = "Happiness Levels by Country", # Chart title
xaxis = list(title = "Happiness Level"), # X-axis label
yaxis = list(title = "Percentage"), # Y-axis label
legend = list(title = list(text = "Country")) # Legend title
)
plot
Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodali [... truncated]
Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodali [... truncated]
Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodali [... truncated]
Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodali [... truncated]
Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodali [... truncated]
Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodali [... truncated]
plot <- plot_ly() %>%
add_trace(
data = op_df,
x = ~happy,
y = ~perc,
color = ~cntry,
type = 'bar',
barmode = 'group',
#name = 'Percentage',
text = ~paste0(
"Country:", cntry,
"<br>Happiness Score:", happy,
"<br>Percentage:", perc, "%",
"<br>Accumulated Frequency:", accum_freq, "%"), # Hover information with details
hoverinfo = 'text', textposition = 'none') %>% # Enable hover with custom text
add_trace(
data = op_df,
x = ~happy,
y = ~accum_freq,
color = ~cntry,
type = 'scatter',
mode = 'lines+markers',
#name = 'Cumulative Frequency',
text = ~paste("Country:", cntry,
"<br>Happy Level:", happy,
"<br>Percentage:", perc, "%",
"<br>Accumulated Frequency:", accum_freq, "%"), # Hover information with details
hoverinfo = 'text', textposition = 'none') %>%
layout(
title = "Grouped Bar Chart with Cumulative Line",
xaxis = list(title = "Happiness Scores"),
yaxis = list(title = "Percentage"),
legend = list(title = list(text = "Country"))
)
plot
Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodali [... truncated]
Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodali [... truncated]
Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodali [... truncated]
Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodali [... truncated]
Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodali [... truncated]
Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodali [... truncated]
Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodali [... truncated]
Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodali [... truncated]
df <- read.csv("Data/ess_allyr_multi.csv")
df <- df %>%
mutate(name = case_when(
name == "ESS1e06_7" ~ "2002",
name == "ESS2e03_6" ~ "2004",
name == "ESS3e03_7" ~ "2006",
name == "ESS4e04_6" ~ "2008",
name == "ESS5e03_5" ~ "2010",
name == "ESS6e02_6" ~ "2012",
name == "ESS7e02_3" ~ "2014",
name == "ESS8e02_3" ~ "2016",
name == "ESS9e03_2" ~ "2018",
name == "ESS10e03_2" ~ "2020",
name == "ESS10SCe03_1" ~ "2020",
name == "ESS11e02" ~ "2023"
))
dfhappy <- df %>%
select(name, cntry, happy) %>%
filter(happy <= 10) %>%
group_by(name, cntry, happy) %>%
summarise(qty = n(), .groups = "drop") %>%
group_by(name, cntry) %>%
mutate(perc = round((qty / sum(qty)) * 100, 1))
filtered <- dfhappy %>%
filter(cntry == c('NL', 'PT'), name == '2023') %>%
group_by(cntry) %>%
mutate(accum_freq = cumsum(perc)) %>%
mutate(accum_freq = ifelse(row_number() == n(), 100, accum_freq)) # Ensure last value = 100
# Calculate metrics relative to happiness level 5
metrics <- filtered %>%
filter(happy == 5) %>%
mutate(towards_unhappy = accum_freq,
towards_happy = 100 - accum_freq)
metrics
plot_ly(data = happy_health, x = ~happy_health$happy) #, y = ~happy_health$health)
No trace type specified:
Based on info supplied, a 'histogram' trace seems appropriate.
Read more about this trace type -> https://plotly.com/r/reference/#histogram
No trace type specified:
Based on info supplied, a 'histogram' trace seems appropriate.
Read more about this trace type -> https://plotly.com/r/reference/#histogram
NA